/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.caro.utils;

/**
 *
 * @author PhucAnh
 */
public class CheckWinner {
    public static int winner(int arr[][],int x,int y,int value){
        int valueToRate = value-1;
       int  g1 = goc1(arr,x,y);
       int  g2 = goc2(arr,x,y);
       int  g3 = goc3(arr,x,y);
       int  g4 = goc4(arr,x,y);
       int  g5 = goc5(arr,x,y);
       int  g6 = goc6(arr,x,y);
       int  g7 = goc7(arr,x,y);
       int  g8 = goc8(arr,x,y);
       if ((g1+g5) >= valueToRate  || (g2 + g6)>=valueToRate  || (g3+g7) >= valueToRate  || (g4+g8)>=valueToRate ) {
           return arr[x][y];
       }
       return -1;
    }
    private  static int goc1(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (x>0){
            x--;
            if(x==-1 ) return 0; 
            while(x>=0) {
                if (arr[x][y] != s) break;
                dem++;
                x--;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc2(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (x>0 && y<Constants.MAX_COLUMNS-1){
            x--;
            y++;
            if(x==-1 || y==20) return 0; 
            while(x>=0 && y<Constants.MAX_COLUMNS) {
                if (arr[x][y] != s) break;
                dem++;
                x--;
                y++;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc3(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (y<Constants.MAX_COLUMNS-1){
//            x--;
            y++;
            if( y==20) return 0; 
            while(y<Constants.MAX_COLUMNS) {
                if (arr[x][y] != s) break;
                dem++;
//                x--;
                y++;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc4(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (x<Constants.MAX_ROWS-1 && y<Constants.MAX_COLUMNS-1){
            x++;
            y++;
            if(x==20 || y==20) return 0; 
            while(x<Constants.MAX_ROWS && y<Constants.MAX_COLUMNS) {
                if (arr[x][y] != s) break;
                dem++;
                x++;
                y++;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc5(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (x<Constants.MAX_ROWS-1){
            x++;
//            y++;
            if(x==Constants.MAX_ROWS) return 0; 
            while(x<Constants.MAX_ROWS) {
                if (arr[x][y] != s) break;
                dem++;
                x++;
//                y++;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc6(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (y>0 && x<Constants.MAX_ROWS-1){
            x++;
            y--;
            if( y==-1||x==20) return 0; 
            while(y>=0 && x<Constants.MAX_ROWS) {
                if (arr[x][y] != s) break;
                dem++;
                x++;
                y--;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc7(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (y>0){
//            x--;
            y--;
            if( y==-1) return 0; 
            while(y>=0) {
                if (arr[x][y] != s) break;
                dem++;
//                x--;
                y--;
            }
            return dem;
        }
        else return dem;
    }
    private static int goc8(int arr[][],int x,int y){
        int dem = 0;
        int s = arr[x][y];
        if (x<Constants.MAX_ROWS-1 && y<Constants.MAX_COLUMNS-1){
            x--;
            y--;
            if(x==-1 || y==-1) return 0; 
            
            while(x>=0&& y>=0) {
                if (arr[x][y] != s) break;
                dem++;
                x--;
                y--;
            }
            return dem;
        }
        else return dem;
    } 
}
